package com.zbkj.crmeb.task.order;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.enums.BarCodeEnum;
import com.zbkj.crmeb.order.StoreOrderFactory;
import com.zbkj.crmeb.store.dao.StoreOrderDao;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.OrderTaskService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@Configuration // 读取配置
public class PushChannelsOrder {
	@Value("${order.push.buyer}")
	private boolean buyerPush;
	@Value("${order.push.europe}")
	private boolean europePush;
	@Value("${order.push.vietti}")
	private boolean viettiPush;
	@Value("${order.push.leam}")
	private boolean leamPush;
	@Value("${order.push.bellettini}")
	private boolean bellettiniPush;
	@Value("${order.push.jky}")
	private boolean jkyPush;
	@Value("${order.push.gb}")
	private boolean gbPush;
	@Value("${order.push.carotta}")
	private boolean carottaPush;
	@Value("${order.push.im}")
	private boolean imPush;
	@Value("${order.push.lavita}")
	private boolean lavitaPush;
	@Value("${order.push.orange}")
	private boolean orangePush;
	@Value("${order.push.unibuy}")
	private boolean unibuyPush;
	@Value("${order.push.cris}")
	private boolean crisPush;
	@Value("${order.push.licheng}")
	private boolean lichengPush;
	@Value("${order.push.efasion}")
	private boolean efasionPush;
	@Value("${order.push.daigo}")
	private boolean daigoPush;

	@Resource
	private StoreOrderDao storeOrderDao;
	@Autowired
	private StoreOrderFactory storeOrderFactory;
	@Autowired
	private OrderTaskService orderTaskService;
	@Autowired
	private IChannelMerchantService channelMerchantService;
//	@Value("${order.find.im}")
//	private boolean imFind;

	// 5分钟查询订单表 推送渠道下单
	@XxlJob("pushChannelsOrder-init") 
	public void init() {
		//查询自动推送的商户
		List<ChannelMerchant> merchantList = channelMerchantService.list(Wrappers.<ChannelMerchant>lambdaQuery()
				.eq(ChannelMerchant::getAutoPush, 1)
				.select(ChannelMerchant::getId));
		if(CollectionUtils.isEmpty(merchantList)){
			return;
		}
		List<Integer> merIdList = merchantList.stream().map(ChannelMerchant::getId).collect(Collectors.toList());
		LambdaQueryWrapper<StoreOrder> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StoreOrder::getPaid, 1); // 已支付
		lqw.and(e -> e.eq(StoreOrder::getStatus, 1).or().eq(StoreOrder::getStatus, 6)); // 待发货
		lqw.eq(StoreOrder::getPushStatus, 0); // 未推送
		lqw.eq(StoreOrder::getIsDel, 0); // 未删除
		lqw.eq(StoreOrder::getIsSystemDel, 0); // 后台未删除
		lqw.eq(StoreOrder::getAbnormal, 0);
		lqw.in(StoreOrder::getMerId,merIdList);
		List<StoreOrder> orderList = storeOrderDao.selectList(lqw);
		List<String> orderIds = orderList.stream().map(StoreOrder::getOrderId).collect(Collectors.toList());
		log.info("--------------- 本次待推送订单号:{} ---------------", JSONArray.toJSONString(orderIds));
		if (CollectionUtils.isNotEmpty(orderList)) {
			orderList.stream().forEach(order -> {
				// 推送buyer订单
//                if (buyerPush) {
//                    storeOrderFactory.pushOrder(order, BarCodeEnum.BUYER, true);
//                }
				// 推送欧洲订单
//                if(europePush) {
//                    storeOrderFactory.pushOrder(order, BarCodeEnum.EUROPE, true);
//                }
				// 推送vietti订单
				if (viettiPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.VIETTI, true);
				}
				// 推送efasion订单
				if (efasionPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.EFASION, true);
				}
				// 推送LEAM订单
				if (leamPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.LEAM, true);
				}
				// 推送BELLECTTINI订单
				if (bellettiniPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.BELLETTINI, true);
				}
				// 推送吉客云订单
				if (jkyPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.JKY, true);
				}
				// 推送G&B订单
				if (gbPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.GB, true);
				}
				// 推送CAROTTA订单
				if (carottaPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.CAROTTA, true);
				}
				// 推送IM订单
				if (imPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.IM, true);
				}
				// 推送Lavita订单
				if (lavitaPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.LAVITA, true);
				}
				// 推送orange订单
				if (orangePush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.ORANGE, true);
				}
				// 推送unibuy订单
				if (unibuyPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.UNIBUY, true);
					storeOrderFactory.pushOrder(order, BarCodeEnum.UNIBUY1, true);
				}
				// 推送里橙订单(只有采购单)
				if (lichengPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.LICHENG, true);
				}
				// 推送cris订单
				if (crisPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.CRIS, true);
				}
				// 推送万象订单
				if (daigoPush) {
					storeOrderFactory.pushOrder(order, BarCodeEnum.DAIGO, true);
				}
			});
		}
	}

	/**
	 * 查询回调失败的订单重试, 每隔三十分钟 @Description: @author daigb @param @return void @throws
	 */
	@XxlJob("pushChannelsOrder-callBackOrder")
	public void callBackOrder() {
		// 查询回调失败并且次数少于三次的订单
		List<StoreOrder> storeOrders = storeOrderDao
				.selectList(Wrappers.<StoreOrder>lambdaQuery().eq(StoreOrder::getBackStatus, false)
						.lt(StoreOrder::getBackCount, 5).isNotNull(StoreOrder::getBackCount));
		log.info("--------------- 本次待回调订单数量:{} ---------------", storeOrders == null ? 0 : storeOrders.size());
		if (CollectionUtils.isNotEmpty(storeOrders)) {
			orderTaskService.callBackOrder(storeOrders);
		}
	}

	// 5分钟查询订单表 获取接口订单状态更新未确认的
	@XxlJob("getOrderInfo")
	public void getOrderInfo() {

		LambdaQueryWrapper<StoreOrder> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StoreOrder::getPaid, 1); // 已支付
		lqw.eq(StoreOrder::getStatus, 6); // 待确认
		lqw.eq(StoreOrder::getPushStatus, 1); // 已推送
		lqw.eq(StoreOrder::getIsDel, 0); // 未删除
		lqw.eq(StoreOrder::getIsSystemDel, 0); // 后台未删除
		lqw.eq(StoreOrder::getAbnormal, 0);

		List<StoreOrder> orderList = storeOrderDao.selectList(lqw);
		List<String> orderIds = orderList.stream().map(StoreOrder::getOrderId).collect(Collectors.toList());
		log.info("--------------- 本次待查询状态的订单号:{} ---------------", JSONArray.toJSONString(orderIds));
		if (CollectionUtils.isNotEmpty(orderList)) {
			orderList.stream().forEach(order -> {

//				// 推送vietti订单
//				if (viettiPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.VIETTI, true);
//				}
//				// 推送efasion订单
//				if (efasionPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.EFASION, true);
//				}
//				// 推送LEAM订单
//				if (leamPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.LEAM, true);
//				}
//				// 推送BELLECTTINI订单
//				if (bellettiniPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.BELLETTINI, true);
//				}
//				// 推送吉客云订单
//				if (jkyPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.JKY, true);
//				}
//				// 推送G&B订单
//				if (gbPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.GB, true);
//				}
//				// 推送CAROTTA订单
//				if (carottaPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.CAROTTA, true);
//				}
				// 获取IM订单
				if (imPush) {
					storeOrderFactory.updateOrderInfo(order, BarCodeEnum.IM);
				}
//				// 推送Lavita订单
//				if (lavitaPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.LAVITA, true);
//				}
//				// 推送orange订单
//				if (orangePush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.ORANGE, true);
//				}
//				// 推送unibuy订单
//				if (unibuyPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.UNIBUY, true);
//					storeOrderFactory.pushOrder(order, BarCodeEnum.UNIBUY1, true);
//				}
//				// 推送里橙订单(只有采购单)
//				if (lichengPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.LICHENG, true);
//				}
//				// 推送cris订单
//				if (crisPush) {
//					storeOrderFactory.pushOrder(order, BarCodeEnum.CRIS, true);
//				}
			});
		}
	}

	/**
	 * IM订单拉取 @Description: @author daigb @param @return void @throws
	 */
	@XxlJob("pushChannelsOrder-imPutOrder")
	public void imPutOrder() {
		log.info("--------------- 开始拉取IM订单数据 ---------------");
		orderTaskService.imPutOrder();
	}

}
